﻿using Hotel.Models;
using IHotelBLL.ICheckInBLL;
using IHotelDAL.ICheckInDAL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Hotel.Controllers.CheckIn.CheckIn
{
    public class CheckInController : Controller
    {
        ResultModel result = new ResultModel();
        ICheckInBLL _checkInBLL;
        public CheckInController(ICheckInBLL checkInBLL)
        {
            _checkInBLL = checkInBLL;
        }
        public IActionResult GetCheckInOptions()
        {
            result.Data = _checkInBLL.GetRoomTypeOptions();
            result.Status = 1;
            result.IsSuccess = true;
            if (result.Data is null)
            {
                result.Msg = "未找到客房信息";
                result.IsSuccess = false;
            }

            return Json(result);
        }
        public IActionResult GetCheckInfoById(string Id)
        {
            result.Data = _checkInBLL.GetCheckIn(Id);
            return Json(result);
        }
        public IActionResult CheckIn(string roomId, decimal amount,string Name, string ID_Card)
        {
            var userId = HttpContext.Session.GetString("employeeId");
            if (roomId is null)
            {
                result.Msg = "未找到房间";
                return Json(result);
            }
            if (ID_Card is null)
            {
                result.Msg = "请输入身份证号";
                return Json(result);
            }
            result.IsSuccess = _checkInBLL.AddCheckInRecord(roomId, amount, userId,ID_Card, Name,out string msg);
            result.Msg = msg;
            return Json(result);
        }
        public IActionResult AppointmentCheckIn(string appointmentId)
        {
            if (appointmentId == null)
            {
                result.Msg = "Id不能为空";
            }
            string creator = HttpContext.Session.GetString("employeeId");
            result.IsSuccess = _checkInBLL.AddAppointmentCheckInRecord(creator, appointmentId, out string msg);
            result.Msg = msg;
            return Json(result);
        }
        public IActionResult CheckOut(string id)
        {
            result.IsSuccess = _checkInBLL.CheckOut(id, out string msg);
            result.Msg = msg;
            return Json(result);
        }
        public IActionResult ChangeTheRoom(string id, string roomId, string reason)
        {
            if (id is null)
            {
                result.Msg = "未找到用户信息";
                return Json(result);
            }
            result.IsSuccess = _checkInBLL.ChangeTheRoom(id, roomId, reason, out string msg);
            result.Msg = msg;
            return Json(result);
        }
        public IActionResult GetCheckIn(int page,int limit)
        {
            var checkIn = _checkInBLL.GetCheckIn(page, limit, out int count);
            var res = new
            {
                data = checkIn,
                count,
                code = 0
            };
            return Json(res);
        }




        /// <summary>
        /// 根据id查询信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult GetcheckById(string id)
        {
            //返回对象
            ResultModel res = new ResultModel();
            if (string.IsNullOrEmpty(id))
            {
                res.Msg = "id不能为空";
                return Json(res);
            }

            //根据id查类别信息
            Entity.CheckInEntity.CheckIn checkIn = _checkInBLL.GetCheckIn(id);
            if (checkIn == null)
            {
                res.Msg = "未查询到信息";
                return Json(res);
            }



            res.Msg = "成功";
            res.Status = 1;
            res.Data = new
            {
                checkIn = checkIn,
              
            };
            res.IsSuccess = true;
            return Json(res);
        }


        [HttpPost]
        public IActionResult UpdateCheckIn(string id, decimal amount, string name, string iD_Card)
        {
            //返回对象
            ResultModel res = new ResultModel();
            if (string.IsNullOrEmpty(id))
            {
                res.Msg = "id不能为空";
                return Json(res);
            }
            if (string.IsNullOrEmpty(name))
            {
                res.Msg = "姓名不能为空";
                return Json(res);
            }

            string msg;
            //调用bll修改类别业务方法
            bool isSuccess = _checkInBLL.UpdateCheckIn(id, amount, name, iD_Card , out msg);

            if (isSuccess)
            {
                res.Status = 1;
                res.IsSuccess = true;
            }
            res.Msg = msg;
            return Json(res);
        }
    }
}
